/**
 * @author LiuYan
 * @date 2021/11/15 8:21
 */
public class Part08 {
    public static void main(String[] args) {
        // String字符串是不可变的，采用拼接的方式会在内存中产生大量的对象并且效率低
        long startTime = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < 9999; i++) {
            str += i;
        }
        long endTime = System.currentTimeMillis();
        System.out.println(String.format("string字符串拼接花费时间: %s毫秒", endTime - startTime));
        //StringBuffer和StringBuilder是可变的字符串，不会产生大量对象并且效率高
        long startBufferTime = System.currentTimeMillis();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 99999; i++) {
            stringBuffer.append(i);
        }
        long endBufferTime = System.currentTimeMillis();
        System.out.println(String.format("stringBuffer字符串拼接花费时间: %s毫秒", endBufferTime - startBufferTime));


        long startBuilderTime = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 99999; i++) {
            stringBuilder.append(i);
        }
        long endBuilderTime = System.currentTimeMillis();
        System.out.println(String.format("stringBuilder字符串拼接花费时间: %s毫秒", endBuilderTime - startBuilderTime));
        /**
         * StringBuffer和StringBuilder：
         * append 链式追加调用
         * insert 指定位置插入元素
         * delete删除指定位置的元素
         */
    }
}
